<template>
  <list-card>
    <template v-slot:search>
      <el-form ref="queryForm" :model="queryParams" inline size="small">
        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="queryParams.userName" clearable placeholder="请输入用户名称"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item v-if="showSearch" label="登录账号" prop="userCode">
          <el-input v-model="queryParams.userCode" clearable placeholder="请输入用户名称"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item v-if="showSearch" label="登录结果" prop="result">
          <el-select v-model="queryParams.result" clearable placeholder="登录结果">
            <el-option v-for="item in localDict.system_result" :key="item.value" :label="item.label"
                       :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item v-if="showSearch" label="设备类型" prop="deviceType">
          <el-select v-model="queryParams.deviceType" clearable placeholder="登录结果">
            <el-option v-for="item in localDict.device_type" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item v-if="showSearch" label="授权类型" prop="grantType">
          <el-select v-model="queryParams.grantType" clearable placeholder="登录结果">
            <el-option v-for="item in localDict.grant_type" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="登录时间">
          <el-date-picker v-model="queryParams.loginTime" :default-time="['00:00:00', '23:59:59']"
                          type="daterange" start-placeholder="开始日期" range-separator="-" end-placeholder="结束日期"
                          value-format="yyyy-MM-dd HH:mm:ss" :picker-options="pickerOptions"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </template>

    <template v-slot:operate>
      <el-button v-has-permi="['monitor:login:remove']" :disabled="multiple" icon="el-icon-delete" plain size="mini"
                 type="danger" @click="handleDelete">删除
      </el-button>
      <!--      <el-button v-has-permi="['monitor:login:export']" icon="el-icon-download" plain size="mini" type="warning"-->
      <!--                 @click="handleExport">导出-->
      <!--      </el-button>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"/>
    </template>
    <template v-slot:content>
      <el-table v-loading="loading" :data="dataList" row-key="id" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column label="用户姓名" prop="userName" min-width="140" show-overflow-tooltip/>
        <el-table-column label="登录账号" prop="userCode" min-width="150" show-overflow-tooltip/>
        <el-table-column label="登录时间" prop="loginTime" min-width="180" show-overflow-tooltip/>
        <el-table-column label="设备类型" prop="deviceType" min-width="150">
          <template v-slot:default="scope">
            <dict-tag :options="localDict.device_type" :value="scope.row.deviceType"/>
          </template>
        </el-table-column>
        <el-table-column label="授权方式" prop="grantType" align="center" min-width="160">
          <template v-slot:default="scope">
            <dict-tag :options="localDict.grant_type" :value="scope.row.grantType"/>
          </template>
        </el-table-column>
        <el-table-column label="操作系统" prop="os" min-width="220" show-overflow-tooltip/>
        <el-table-column label="登录结果" prop="result" min-width="120" align="center">
          <template v-slot:default="scope">
            <dict-tag :options="localDict.system_result" :value="scope.row.result"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="120" fixed="right">
          <template v-slot:default="scope">
            <el-button v-has-permi="['monitor:login:remove']" icon="el-icon-delete" size="mini" type="text"
                       @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="pageParam.total > 0" :total="pageParam.total" :limit.sync="pageParam.pageSize"
                  :page.sync="pageParam.pageNum" @pagination="getList"/>
    </template>
  </list-card>
</template>

<script>
import moment from "moment";
import {clean, list, remove} from "../../../api/base";
import listMixin from "../../../mixins/listMixin";

export default {
  name: "Logininfor",
  components: {},
  mixins:{listMixin},
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: true,
      baseUrl: '/monitor/login',
      dataList: [],
      pageParam: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      queryParams: {
        result: undefined,
        userName: undefined,
        userCode: undefined,
        grantType: undefined,
        deviceType: undefined,
        loginTime: [],
      },
      localDict: {
        grant_type: [],
        device_type: [],
        system_result: []
      },
      // 显示搜索条件
      showSearch: false,
    };
  },
  created() {
    this.getLocalDict();
    this.resetQuery()
  },
  methods: {
    async getLocalDict() {
      await this.getStaticData('system-device-type').then(res => {
        this.localDict.device_type = res.data
      })
      await this.getStaticData('system-grant-type').then(res => {
        this.localDict.grant_type = res.data
      })
      await this.getStaticData('system-result').then(res => {
        this.localDict.system_result = res.data
      })
    },
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(response => {
          this.dataList = response.rows;
          this.pageParam.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        params: {
          loginTime: undefined,
        }
      }
      this.resetForm("queryForm");
      this.handleQuery()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的访问记录？').then(() => {
        remove(this.baseUrl, infoIds, infoIds.length > 1).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      });
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$modal.confirm('是否确认清空所有登录日志数据项？').then(() => {
        clean(this.baseUrl, this.queryParams).then(() => {
          this.getList();
          this.$modal.msgSuccess("清空成功");
        }).catch(() => {
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(`${this.baseUrl}export`, {
        ...this.queryParams
      }, `系统访问日志_${moment().format('YYYY-MM-DD')}.xlsx`)
    }
  }
};
</script>

